//
//  MainHomeSudokuTableViewCell.m
//  ERP
//
//  Created by QA on 16/4/6.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MainIndustrialSudokuCell.h"

#import "JZHorizontalBtnView.h"
#import "ModuleAuthorityEntity+CoreDataClass.h"

#import "WebViewController.h"
#import "GEPushViewControllerHelper.h"
#import "UIColorDispose.h"

#define screen_width [UIScreen mainScreen].bounds.size.width
#define ItemHeight 66

@interface MainIndustrialSudokuCell ()
{
    UINavigationController *navigationController;
    NSArray *menuValueArray;
}

@end

@implementation MainIndustrialSudokuCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

+ (CGFloat)getHeightItemCount:(NSInteger)count
{
    return (count/2 + count%2) * ItemHeight;
}

- (void)setCuttingLineItemCount:(NSInteger)count
{
    CGFloat lineCount = count/2 + count%2;
    CGFloat width = screen_width;
    
    //绘制竖线
    UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(width/2, 0, 0.5, ItemHeight*lineCount)];
    verticalLine.backgroundColor = [UIColorDispose colorWithHexString:@"dddddd"];
//    verticalLine.backgroundColor = [UIColor colorWithWhite:1 alpha:0.85];
    [self addSubview:verticalLine];
    
    //绘制水平
    for(int i=0; i<lineCount; i++){
        UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(0, ItemHeight*i, width, 0.5)];
        verticalLine.backgroundColor = [UIColorDispose colorWithHexString:@"dddddd"];
//        verticalLine.backgroundColor = [UIColor colorWithWhite:1 alpha:0.85];
        [self addSubview:verticalLine];
    }
}

-(id)setMenuArray:(NSArray *)menuArray navigation:(UINavigationController*)navigation
{
    navigationController = navigation;
    menuValueArray = menuArray;
    
    CGFloat width = screen_width;
    CGFloat height = [MainIndustrialSudokuCell getHeightItemCount:menuArray.count];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    backView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8f];
    
    //创建2个
    for (int i = 0; i < menuArray.count; i++) {
        if (i%2 != 0) {
            CGRect frame = CGRectMake(width/2, i/2*ItemHeight, width/2, ItemHeight);
            ModuleAuthorityEntity *entity = [menuArray objectAtIndex:i];
            
            JZHorizontalBtnView *btnView = [[JZHorizontalBtnView alloc] initWithFrame:frame title:entity.title subLable:entity.descripter imageStr:entity.icon];
            btnView.tag = i;
            btnView.backgroundColor = [UIColor clearColor];
            [backView addSubview:btnView];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
            [btnView addGestureRecognizer:tap];
            
        } else {
            CGRect frame = CGRectMake(0, i/2*ItemHeight, width/2, ItemHeight);
            ModuleAuthorityEntity *entity = [menuArray objectAtIndex:i];
            
            JZHorizontalBtnView *btnView = [[JZHorizontalBtnView alloc] initWithFrame:frame title:entity.title subLable:entity.descripter imageStr:entity.icon];
            btnView.tag = i;
            btnView.backgroundColor = [UIColor clearColor];
            [backView addSubview:btnView];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
            [btnView addGestureRecognizer:tap];
        }
    }
    
    [self addSubview:backView];
    [self setCuttingLineItemCount:menuArray.count];
    return self;
}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender{
    NSLog(@"tag:%ld",sender.view.tag);
    
    ModuleAuthorityEntity *entity = [menuValueArray objectAtIndex:sender.view.tag];
    if(entity.codeType.integerValue == 1){
        //网页模式
        WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
        model.URL = entity.webCode;
        model.Title = entity.title;
        model.CodeType = CodeTypeWeb;
        
        WebViewController *viewController = [[WebViewController alloc] initWithPushParameterModel:model];
        [navigationController pushViewController:viewController animated:YES];
    } else {
        //原生APP模式
        [GEPushViewControllerHelper pushViewControllerHomePageCode:entity.nativeCode navigation:navigationController];
    }
}

@end
